#include <iostream>
#include <future>
#include <chrono>

int Add(int x, int y)
{
    std::cout << "加法     1" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout << "加法     2" << std::endl;
    return x + y;
}

int main()
{
    std::cout << "---------------1----------------" << std::endl;
    // std::launch::deferred 在执行get获取异步结果时才执行异步任务
    // std::launch::async 内部会创建工作线程，异步地完成任务
    // std::future<int> result = std::async(std::launch::deferred, Add, 1, 2);
    std::future<int> result = std::async(std::launch::async, Add, 1, 2);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "---------------2----------------" << std::endl;
    int sum = result.get();
    std::cout << "---------------3-----------------" << std::endl;
    std::cout << sum << std::endl;
    return 0;
}